package zcjq.d831;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MySort {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = Integer.parseInt(scanner.nextLine());
        List<String> strList = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            strList.add(scanner.nextLine());
        }
        // 排序
        Collections.sort(strList, (str1, str2) -> {
            System.out.println(str1 + "-" + str2);
            String rStr1 = revert(str1);
            String rStr2 = revert(str2);
            int a1 = str1.compareTo(str2);
            int a2 = str1.compareTo(rStr2);
            int a3 = rStr1.compareTo(str2);
            int a4 = rStr1.compareTo(rStr2);
            if ((a1 < 0 && a2 < 0) || (a3 < 0 && a4 < 0)) { //str1<str2
                return -1;
            } else if ((a1 > 0 && a3 > 0) || (a2 > 0 && a4 > 0)) {
                return 1;
            } else {
                return 0;
            }
        });
        System.out.println(strList);
    }

    public static String revert(String str) {
        char[] chars = str.toCharArray();
        int left = 0, right = str.length() - 1;
        while (left < right) {
            char tmp = chars[left];
            chars[left] = chars[right];
            chars[right] = tmp;
            left++;
            right--;
        }
        return new String(chars);
    }
}
